<?php
if($_SESSION['login'] != true) {
	die('Access danied');
}
//------------------------------
define(ACTION_SEARCHERS,		"actionSearchers");
//------------------------------
$scriptAddPortalNumber = 0;
//------------------------------
$db->query("SELECT value FROM \"" . $configDbPrefix . "config\" WHERE name = 'CRAWLER_IS_WORKING' AND config_type = 'WORK';");
$rows		= $db->fetchArray();
$row 		= $rows[0];
$appState 	= $row['value'];
//------------------------------
if(IsSet($_GET['action'])) {
	$action = $_GET['action'];
	
}
//------------------------------
$STATE_OK = 'OK';
$STATE_ERROR = 'ERROR';
$proxiesNumberOk = getStateCount($STATE_OK);
$proxiesNumberError = getStateCount($STATE_ERROR);

$db->query("SELECT proxy_id, address, port, state, last_checked, info, type FROM \"" . $configDbPrefix . "proxies\" ORDER BY proxy_id;");
$rows	= $db->fetchArray();

insertMenu(0);
//insertAppState(0);
//insertAddNewPortal(0);
?>
<table>
	<tr class="bgWhite">
		<td></td>
		<td>Działające</td>
		<td><?php echo $proxiesNumberOk; ?></td>
	</tr>
	<tr class="bgGray">
		<td></td>
		<td>Niedziałające</td>
		<td><?php echo $proxiesNumberError; ?></td>
	</tr>
</table>
<br /><br />
<table>
	<tr class="bgWhite">
		<th></td>
		<th>Adres</th>
		<th>Typ</th>
		<th>Info</th>
		<th>Ostatnio sprawdzany</th>
		<!--<th colspan="1" style="text-align: center;">Opcje</th>-->
	</tr>
	<?php
	$i = 0;
	foreach($rows as $row) {
		$proxyId = $row['proxy_id'];
		$i++;
		if($i % 2 == 0) {
			?>
			<tr class="bgWhite">
			<?php
		} else {
			?>
			<tr class="bgGray">
			<?php
		}
		?>
			<!-- kolejny numer proxy -->
			<td width="3%"><?php echo $i . '.' ?></td>
			<!-- adres -->
			<td>
				<?php 
					$state = $row['state'];
					if($state == $STATE_OK) {
						?><div class="activeOn"><?php
					} else {
						?><div class="activeOff"><?php
					}
				?>
				<?php echo $row['address'] . ':' . $row['port']; ?>
				</div>
			</td>
			<!-- typ -->
			<td>
				<?php echo $row['type']; ?>
			</td>
			<!-- informacje -->
			<td>
				<?php echo $row['info']; ?>
			</td>
			<!-- ostatnio sprawdzany -->
			<td>
				<?php echo $row['last_checked']; ?>
			</td>
		</tr>
		<?php
	}
	?>
</table>
<br>
<?php
//insertAddNewPortal(1);
//insertAppState(1);
insertMenu(1);
//enableConfirm();

/*
*	FUNKCJE
*/

function insertAddNewPortal($idN) {
	$link = $PHP_SELF . '?configType=' . MONITOR_AND_MANAGE . '&action=' . ACTION_ADD;
	?>
	<form method="POST" action="<?php echo $link ?>" id="addNewPortal<?php echo $idN ?>" style="display:none;">
		Nazwa portalu: <input type="text" name="portalName">&nbsp;
		Adres portalu: <input type="text" name="portalUrl" value="http://">&nbsp;
		<button type="submit"><img src="images/add.gif">&nbsp;Dodaj nowy portal</button>
	</form>
	<?php
}

function insertAppState($idN) {
	$linkStop = $PHP_SELF . '?configType=' . MONITOR_AND_MANAGE . '&action=' . ACTION_SHUTTING_DOWN;
	$linkStart= $PHP_SELF . '?configType=' . MONITOR_AND_MANAGE . '&action=' . ACTION_STARTING_UP;
	global $appState;
	?>
	<div id="appState<?php echo $idN ?>" style="display: none;">
		Obecnie aplikacja jest: <?php echo mapStateCodeIntoName($appState) ?>
		&nbsp;&nbsp;&nbsp;
		<?php
		if($appState == 'YES') {
			?>
			<a href="<?php echo $linkStop ?>"><img src="images/stop.gif">&nbsp;Zatrzymaj aplikację</a>
			<?php
		} else if($appState == 'NO') {
			?>
			<!--<a href="<?php echo $linkStart?>"><img src="images/start.gif">&nbsp;Uruchom aplikację</a>-->
			<?php
		}
		?>
		<br>
		<br>
	</div>
	<?php
}

function insertMenu($idN) {
	?>
	<script type="text/javascript">
		$(document).ready(
			function(){
				$("#addNewPortalLink<?php echo $idN ?>").click(
					function(){
						$("#addNewPortal<?php echo $idN ?>").toggle("normal");
					}
				);
			}
		);
	</script>
	<script type="text/javascript">
		$(document).ready(
			function(){
				$("#appStateLink<?php echo $idN ?>").click(
					function(){
						$("#appState<?php echo $idN ?>").toggle("normal");
					}
				);
			}
		);
	</script>
	<a href="../"><img src="images/previous.png " alt="Wstecz">&nbsp;Strona główna</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<!--<a href="#" id="addNewPortalLink<?php echo $idN ?>" ><img src="images/add.gif" alt="Dodaj nowy portal">&nbsp;Dodaj nowy portal</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="#" id="appStateLink<?php echo $idN ?>" ><img src="images/action.gif" alt="Dodaj nowy portal">&nbsp;Stan aplikacji</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="<?php echo $PHP_SELF . '?configType=' . MONITOR_AND_MANAGE . '&actionType=' . ACTION_TYPE_STATISTICS ?>"><img src="images/statistics.gif" alt="Statystyki">&nbsp;Statystyki ogólne</a>-->
	<br>
	<br>
	<?php
}

function mapStateCodeIntoName($appState) {
	if($appState == 'NO') {
		return '<div style="display: inline; color: red; font-weight: bold;">wyłączona</div>';
	} else if($appState == 'YES') {
		return '<div style="display: inline; color: green; font-weight: bold;">uruchomiona</div>';
	} else if($appState == 'SHUTTING_DOWN') {
		return '<div style="display: inline; color: orange; font-weight: bold;">w czasie zatrzymywania</div>';
	} else {
		return 'nieznany stan';
	}
}

function getStateCount($state) {
	global $db;
	global $configDbPrefix;
	$db->query("SELECT count(*) as liczba FROM \"" . $configDbPrefix . "proxies\" WHERE state='" . $state . "';");
	$rows	= $db->fetchArray();
	$row = $rows[0];
	return $row['liczba'];
}

?>
